package com.app.business.web.business.property;

import com.app.business.model.BuyHousePerson;
import com.app.business.model.CarLot;
import com.app.business.model.House;
import com.app.business.model.StorageRoom;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交房状态数据管理
 * Created by ddz on 2021/9/8.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/property/takeHouse/*")
@SuppressWarnings("all")
public class TakeHouseController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private HouseBuyPersonService houseBuyPersonService;
    @Autowired
    private CarLotBuyPersonService carLotBuyPersonService;
    @Autowired
    private StorageRoomPersonService storageRoomPersonService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;
    @Autowired
    private ActionHistoryService actionHistoryService;
    @Autowired
    private StorageRoomService storageRoomService;
    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private Configuration freemarkerConfiguration; // 注入bean，这里是属性注入，推荐构造注入

    /**
     * 交房数据管理列表
     */
    @RequestMapping("takeHouseList")
    @RequiresPermissions("takeHouse:takeHouseList")
    public void takeHouseList() {
    }

    /**
     * 加载交房数据管理列表
     *
     * @return
     */
    @RequestMapping("loadTakeHouseList")
    @ResponseBody
    @RequiresPermissions("takeHouse:loadTakeHouseList")
    public Map loadTakeHouseList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<House> housePageBean = this.houseService.getTakeHousePageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> settleMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> daffMap = this.dataDictService.getMapByParentNameForValue("档案发放状态");
        Map<String, DataDict> takeHouseMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(houseList)) {
            Map<String, Object> item = null;
            for (House house : houseList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByHouseId(house.getId());

                item.put("houseId", house.getId());
                item.put("contractId", house.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(house.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(house.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(house.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(house.getHouseNumStr()));
                item.put("houseHold", StringUtils.getNotNull(house.getHouseHold()));
                item.put("houseHoldStr", StringUtils.dataDictShow(houseHoldMap.get(StringUtils.getNotNull(house.getHouseHold()))));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(house.getBuildArea()));
                item.put("takeHouseStatus", IntegerUtils.getNotNull(house.getTakeHouseStatus()));
                item.put("takeHouseStatusStr", StringUtils.dataDictShow(takeHouseMap.get(IntegerUtils.getNotNull(house.getTakeHouseStatus()).toString())));
                item.put("takeHouseDate", DateUtils.dateToStringDefault(house.getTakeHouseDate()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }


    /**
     * 物业房源数据管理
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("wyHouseForm")
    @RequiresPermissions("takeHouse:wyHouseForm")
    public void wyHouseForm(HttpServletRequest request, ModelMap modelMap) {
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        List<String> ids = this.houseBuyPersonService.getBuyHousePersonIdListByHouseId(house.getId());
        List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
        if (ids.size() != 0) {
            buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, house.getContract().getId());
        }
        modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
        modelMap.addAttribute("house", house);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
    }


    /**
     * 房源信息保存
     *
     * @param request
     * @return
     */
    @RequestMapping("wyHouseSave")
    @ResponseBody
    @RequiresPermissions("takeHouse:wyHouseSave")
    public Response wyHouseSave(HttpServletRequest request) {
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = houseService.findOne(houseId);
        Precondition.checkAjaxArguement(house != null, "0012", "房源不存在");
        WebUtils.bind(request, house);
        houseService.save(house);

        Response response = new Response();
        response.setCode("0001");
        return response;
    }

    /**
     * 房源信息导出
     */
    @RequestMapping("wyHouseListExcel")
    @RequiresPermissions("takeHouse:wyHouseListExcel")
    public void wyHouseListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<House> housePageBean = this.houseService.getTakeHouseExcelPageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByHouse();
        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("houseList", houseList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("houseHoldMap", houseHoldMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("房源明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/takeHouse/wyHouseListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }

//    /**
//     * 设置房源交房状态（可撤销）
//     *
//     * @param request
//     */
//    @RequestMapping("setTakeHouseStatus")
//    @ResponseBody
//    @RequiresPermissions("takeHouse:setTakeHouseStatus")
//    public synchronized Response setTakeHouseStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
//        House house = this.houseService.findOne(houseId);
//        Precondition.checkAjaxArguement(house != null, "1111", "数据错误！");
//
//        if (house.getTakeHouseStatus().equals(Status.NO.getIndex())) {
//            house.setTakeHouseStatus(Status.YES.getIndex());
//            house.setTakeHouseDate(new Date());
//        } else {
//            house.setTakeHouseStatus(Status.NO.getIndex());
//            house.setTakeHouseDate(new Date());
//        }
//        this.houseService.save(house);
//        this.contractService.setContractTakeHouseStatus(house.getContract());
//        return new Response("0001", "操作成功");
//    }


    /**
     * 储物间交房数据管理列表
     */
    @RequestMapping("takeStorageRoomList")
    @RequiresPermissions("takeHouse:takeStorageRoomList")
    public void takeStorageRoomList() {
    }

    /**
     * 加载储物间交房数据管理列表
     *
     * @return
     */
    @RequestMapping("loadTakeStorageRoomList")
    @ResponseBody
    @RequiresPermissions("takeHouse:loadTakeStorageRoomList")
    public Map loadTakeStorageRoomList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.getDaffStorageRoomPageBean();
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> takeHouseMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(storageRoomList)) {
            Map<String, Object> item = null;
            for (StorageRoom storageRoom : storageRoomList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByStorageRoomId(storageRoom.getId());

                item.put("storageRoomId", storageRoom.getId());
                item.put("contractId", storageRoom.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(storageRoom.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(storageRoom.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(storageRoom.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(storageRoom.getHouseNumStr()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(storageRoom.getBuildArea()));
                item.put("takeHouseStatus", IntegerUtils.getNotNull(storageRoom.getTakeHouseStatus()));
                item.put("takeHouseStatusStr", StringUtils.dataDictShow(takeHouseMap.get(IntegerUtils.getNotNull(storageRoom.getTakeHouseStatus()).toString())));
                item.put("takeHouseDate", DateUtils.dateToStringDefault(storageRoom.getTakeHouseDate()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonTel + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonTel + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(storageRoomPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 已选储物间信息导出
     */
    @RequestMapping("wyStorageRoomListExcel")
    @RequiresPermissions("takeHouse:wyStorageRoomListExcel")
    public void wyStorageRoomListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.getDaffStorageRoomPageBean();
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> financeSettleStatusMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();

        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("storageRoomList", storageRoomList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("financeSettleStatusMap", financeSettleStatusMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("储物间明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/takeHouse/wyStorageRoomListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }

    /**
     * 物业储物间数据管理
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("wyStorageRoomForm")
    @RequiresPermissions("takeHouse:wyStorageRoomForm")
    public void wyStorageRoomForm(HttpServletRequest request, ModelMap modelMap) {
        Integer storageRoomId = ParamUtils.getInt(request, "storageRoomId", 0);
        StorageRoom storageRoom = this.storageRoomService.findOne(storageRoomId);
        List<String> ids = this.storageRoomPersonService.getBuyHousePersonIdListByStorageRoomId(storageRoom.getId());
        List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
        if (ids.size() != 0) {
            buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, storageRoom.getContract().getId());
        }
        modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
        modelMap.addAttribute("storageRoom", storageRoom);
    }


    /**
     * 房源信息保存
     *
     * @param request
     * @return
     */
    @RequestMapping("wyStorageRoomSave")
    @ResponseBody
    @RequiresPermissions("takeHouse:wyStorageRoomSave")
    public Response wyStorageRoomSave(HttpServletRequest request) {
        Integer storageRoomId = ParamUtils.getInt(request, "storageRoomId", 0);
        StorageRoom storageRoom = this.storageRoomService.findOne(storageRoomId);
        Precondition.checkAjaxArguement(storageRoom != null, "0012", "房源不存在");
        WebUtils.bind(request, storageRoom);
        storageRoomService.save(storageRoom);

        Response response = new Response();
        response.setCode("0001");
        return response;
    }

//    /**
//     * 设置储物间交房状态（可撤销）
//     *
//     * @param request
//     */
//    @RequestMapping("setTakeStorageRoomStatus")
//    @ResponseBody
//    @RequiresPermissions("takeHouse:setTakeStorageRoomStatus")
//    public synchronized Response setTakeStorageRoomStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Integer storageRoomId = ParamUtils.getInt(request, "storageRoomId", 0);
//        StorageRoom storageRoom = this.storageRoomService.findOne(storageRoomId);
//        Precondition.checkAjaxArguement(storageRoom != null, "1111", "数据错误！");
//
//        if (storageRoom.getTakeHouseStatus().equals(Status.NO.getIndex())) {
//            storageRoom.setTakeHouseStatus(Status.YES.getIndex());
//            storageRoom.setTakeHouseDate(new Date());
//        } else {
//            storageRoom.setTakeHouseStatus(Status.NO.getIndex());
//            storageRoom.setTakeHouseDate(new Date());
//        }
//        this.storageRoomService.save(storageRoom);
//        this.contractService.setContractTakeHouseStatus(storageRoom.getContract());
//        return new Response("0001", "操作成功");
//    }

    /**
     * 交车位数据管理列表
     */
    @RequestMapping("takeCarLotList")
    @RequiresPermissions("takeHouse:takeCarLotList")
    public void takeCarlotList() {
    }

    /**
     * 加载交房车位数据管理列表
     *
     * @return
     */
    @RequestMapping("loadTakeCarLotList")
    @ResponseBody
    @RequiresPermissions("takeHouse:loadTakeCarLotList")
    public Map loadTakeCarlotList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<CarLot> carLotPageBean = this.carLotService.getDaffCarLotPageBean();
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> takeHouseMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(carLotList)) {
            Map<String, Object> item = null;
            for (CarLot carLot : carLotList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByCarLotId(carLot.getId());

                item.put("carLotId", carLot.getId());
                item.put("contractId", carLot.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(carLot.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(carLot.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(carLot.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(carLot.getHouseNumStr()));
                item.put("carLotType", IntegerUtils.getNotNull(carLot.getCarLotType()));
                item.put("carLotTypeStr", StringUtils.dataDictShow(carLotTypeMap.get(IntegerUtils.getNotNull(carLot.getCarLotType()).toString())));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(carLot.getBuildArea()));
                item.put("takeHouseStatus", IntegerUtils.getNotNull(carLot.getTakeHouseStatus()));
                item.put("takeHouseStatusStr", StringUtils.dataDictShow(takeHouseMap.get(IntegerUtils.getNotNull(carLot.getTakeHouseStatus()).toString())));
                item.put("takeHouseDate", DateUtils.dateToStringDefault(carLot.getTakeHouseDate()));

                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(carLotPageBean.getTotalCount().intValue(), listData);
    }


    /**
     * 已选车位信息导出
     */
    @RequestMapping("wyCarLotListExcel")
    @RequiresPermissions("takeHouse:wyCarLotListExcel")
    public void wyCarLotListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<CarLot> carLotPageBean = this.carLotService.getDaffCarLotExcelPageBean();
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> azSignStatusMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> financeCheckStatusMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> takeHouseStatusMap = this.dataDictService.getMapByParentNameForValue("交房状态");
        Map<String, DataDict> bzStatusMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> zxStatusMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> rzStatusMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, List<Map>> buyHousePersonMap = this.buyHousePersonService.getBuyHousePersonByCarLot();


        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("carLotList", carLotList);
            data.put("azSignStatusMap", azSignStatusMap);
            data.put("financeCheckStatusMap", financeCheckStatusMap);
            data.put("buyHousePersonMap", buyHousePersonMap);
            data.put("carLotTypeMap", carLotTypeMap);
            data.put("takeHouseStatusMap", takeHouseStatusMap);
            data.put("bzStatusMap", bzStatusMap);
            data.put("zxStatusMap", zxStatusMap);
            data.put("rzStatusMap", rzStatusMap);
            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("车位明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/takeHouse/wyCarLotListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }

    /**
     * 物业房源数据管理
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("wyCarLotForm")
    @RequiresPermissions("takeHouse:wyCarLotForm")
    public void wyCarLotForm(HttpServletRequest request, ModelMap modelMap) {
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
        CarLot carLot = this.carLotService.findOne(carLotId);
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        List<String> ids = this.carLotBuyPersonService.getBuyHousePersonIdListByCarLotId(carLot.getId());
        List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
        if (ids.size() != 0) {
            buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, carLot.getContract().getId());
        }
        modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
        modelMap.addAttribute("carLot", carLot);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
    }


    /**
     * 房源信息保存
     *
     * @param request
     * @return
     */
    @RequestMapping("wyCarLotSave")
    @ResponseBody
    @RequiresPermissions("takeHouse:wyCarLotSave")
    public Response wyCarLotSave(HttpServletRequest request) {
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
        CarLot carLot = this.carLotService.findOne(carLotId);
        Precondition.checkAjaxArguement(carLot != null, "0012", "房源不存在");
        WebUtils.bind(request, carLot);
        carLotService.save(carLot);

        Response response = new Response();
        response.setCode("0001");
        return response;
    }

//    /**
//     * 设置车位交房状态（可撤销）
//     *
//     * @param request
//     */
//    @RequestMapping("setTakeCarLotStatus")
//    @ResponseBody
//    @RequiresPermissions("takeHouse:setTakeCarLotStatus")
//    public synchronized Response setTakeCarLotStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
//        CarLot carLot = this.carLotService.findOne(carLotId);
//        Precondition.checkAjaxArguement(carLot != null, "1111", "数据错误！");
//        if (carLot.getTakeHouseStatus().equals(Status.NO.getIndex())) {
//            carLot.setTakeHouseStatus(Status.YES.getIndex());
//            carLot.setTakeHouseDate(new Date());
//        } else {
//            carLot.setTakeHouseStatus(Status.NO.getIndex());
//            carLot.setTakeHouseDate(new Date());
//        }
//        this.carLotService.save(carLot);
//        this.contractService.setContractTakeHouseStatus(carLot.getContract());
//        return new Response("0001", "操作成功");
//    }
//

    /**
     * 装修数据管理列表
     */
    @RequestMapping("zxList")
    @RequiresPermissions("takeHouse:zxList")
    public void zxList() {
    }

    /**
     * 加载装修数据管理列表
     *
     * @return
     */
    @RequestMapping("loadZxList")
    @ResponseBody
    @RequiresPermissions("takeHouse:loadZxList")
    public Map loadZxList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<House> housePageBean = this.houseService.getTakeHousePageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> zxMap = this.dataDictService.getMapByParentNameForValue("装修情况");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(houseList)) {
            Map<String, Object> item = null;
            for (House house : houseList) {
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByHouseId(house.getId());

                item = null;
                item = Maps.newHashMap();
                item.put("houseId", house.getId());
                item.put("contractId", house.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(house.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(house.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(house.getContract().getContractCode()));
                item.put("azProjectName", StringUtils.getNotNull(house.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(house.getHouseNumStr()));
                item.put("houseHold", StringUtils.getNotNull(house.getHouseHold()));
                item.put("houseHoldStr", StringUtils.dataDictShow(houseHoldMap.get(StringUtils.getNotNull(house.getHouseHold()))));
                item.put("zxStatus", IntegerUtils.getNotNull(house.getZxStatus()));
                item.put("zxStatusStr", StringUtils.dataDictShow(zxMap.get(IntegerUtils.getNotNull(house.getZxStatus()).toString())));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(house.getChooseHouseDate()));
                item.put("zxDate", DateUtils.dateToStringDefault(house.getZxDate()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(house.getBuildArea()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));
                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }

//    /**
//     * 设置装修状态（可撤销）
//     *
//     * @param request
//     */
//    @RequestMapping("setZxStatus")
//    @ResponseBody
//    @RequiresPermissions("takeHouse:setZxStatus")
//    public synchronized Response setZxStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
//        House house = this.houseService.findOne(houseId);
//        Precondition.checkAjaxArguement(house != null, "1111", "数据错误！");
//
//        if (house.getZxStatus().equals(Status.NO.getIndex())) {
//            house.setZxStatus(Status.YES.getIndex());
//            house.setZxDate(new Date());
//        } else {
//            house.setZxStatus(Status.NO.getIndex());
//            house.setZxDate(null);
//        }
//        this.houseService.save(house);
//        this.contractService.setContractZxStatus(house.getContract());
//        return new Response("0001", "操作成功");
//    }
}
